import Home from 'src/views/Home'
import User from 'src/views/pages/User'
import Breath from 'src/views/pages/Breath'
import Device from 'src/views/pages/Device'

//异步挂载的路由
//动态需要根据权限加载的路由表 
export const asyncRouterMap = [{
		path: '/',
		auth: true,
		component: Home,
		name: '系统管理',
		iconCls: 'fa fa-bar-chart',
		children: [
			{ path: '/user', component: User, name: '管理员用户管理' },
			{ path: '/test', component: User, name: '不应该出现的栏目' }
		]
	},
	{
		path: '/',
		auth: false,
		component: Home,
		name: '示例',
		iconCls: 'fa fa-bar-chart',
		children: [
			{ path: '/devices', component: Device, name: '设备管理' },
			{ path: '/breath/:deviceId', component: Breath, name: '呼吸监控', hidden: true },
		]
	},
	{
		path: '*',
		auth: false,
		hidden: true,
		redirect: { path: '/404' }
	}
];

//根据可访问目录生成路由
export const generateRoutesFromMenu = (roleMenus) => {
	let accessedRouters = [];

	for (let ar of asyncRouterMap) {
		if (!ar.auth) { //不验证权限
			accessedRouters.push(ar);
		} else if (ar.children && ar.children.length > 0) {
			//筛选匹配子菜单
			ar.children = ar.children.filter(c => {
				return roleMenus.some(m => m == c.path && !c.hidden);
			});
			//子菜单存在
			if (ar.children.length > 0) accessedRouters.push(ar);
		}
	}
	return accessedRouters;
}
